/**
 * @license j2w.socialApply.js
 * Adds social apply button functionality to the base j2w object.
 *
 * Jobs2Web, INC CONFIDENTIAL
 * ------------------
 * Copyright 2003-2012 Jobs2Web, Inc
 * All Rights Reserved.
 * NOTICE: All information contained herein is, and remain the property of
 * Jobs2web, Inc and its suppliers, if any. The intellectual and
 * technical concepts contained herein are proprietary to Jobs2Web, Inc
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material is
 * strictly forbidden unless prior written permission is obtained from
 * Jobs2Web, Inc.
 *
 * @extends j2w.core.js
 *
 */

var j2w = j2w || {};

j2w.SAB = (function () {
    "use strict";

    // *** PRIVATE ************************************************************

    var _args = {},
    _bHasBeenInitted = false,

    // *** METHODS

    _init = function (args) {
        if (!_bHasBeenInitted) {
            _args = args;

            return (_bHasBeenInitted = true);
        } else {
            return false;
        }
    };

    // *** PUBLIC *************************************************************

    return {
        $activeButton : null,
        email : '',
        li : {
            applyStarted : false
        },
        presentation : ['drop', 'pop'],
        profileData : [],

        init : function (args) {
            return _init(args);
        },

        // *** NAMESPACED DEFAULT COLLECTIONS *********************************

        // *** Args - Initialization Arguments ********************************

        Args : {
            get : function (argName) {
                return j2w.Args.get(argName, _args);
            }
        }

    // *** OTHER METHODS **************************************************

    };
}());

$(function () {
    "use strict";

    // Allow configuration of the social apply button presentation methods.
    if (typeof sabConfiguration === 'object' && sabConfiguration.presentation) {
        j2w.SAB.presentation = sabConfiguration.presentation;
    }

    var oSASelected = j2w.SAB.Args.get('profileData')[0];

    // Remove any regular "apply now" buttons on the page.
    $('a.dialogApplyBtn').hide();

    // Prep LinkedIn if it has been loaded.
    if (typeof IN !== 'undefined') {
        IN.Event.on(IN, 'systemReady', sabLinkedInOnLoad);
    }
});

// *********************************************************************
// ** Social Apply Now Button Generic
// *********************************************************************

function hideGetter() {
    $('ul.btnSocialApply .emailGetter').hide();
}

// restoreSocialApplyOptions()
//
function restoreSocialApplyOptions() {
    $('.btnSocialApply ul.sub').find('li').show().filter('li.loading').hide();
}

// sabPrepareTo()
// Positions a social apply button based on its presentation method. (pop|drop)
function sabPrepareTo(intent) {
    switch (intent) {
        case 'pop':
            j2w.SAB.$activeButton.find('ul.sub').css({
                bottom:j2w.SAB.$activeButton.find('a.apply').height() * 2 + 'px',
                left:'',
                top:''
            });
            break;

        default:
            j2w.SAB.$activeButton.find('ul.sub').css({
                bottom:'',
                top:'',
                left:''
            });
            break;
    }
}

// scrollForSABDropDown()
// If the object that was clicked on is the last social apply button on the page, scroll down to ensure all of it is visible.
function scrollForSABDropDown() {
    if ($('ul.btnSocialApply').length > 1 && (j2w.SAB.$activeButton.data('sabID') === $('ul.btnSocialApply').last().data('sabID'))) {
        $('html, body').scrollTop($(document).height());
    }
}

// setMaxWidth()
//
function setMaxWidth($aObjects) {
    var maxWidth = 0;

    // Find the widest of the objects passed in, and save the width value.
    $.each($aObjects,function() {
        $(this).width() > maxWidth ? maxWidth = $(this).width() : null;
    });

    // Set all of the objects to the same width.
    maxWidth ? $aObjects.width(maxWidth + 'px') : null;
}

// *********************************************************************
// ** Facebook-specific Functions
// *********************************************************************

function socialApplyCheckFB() {
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            socialApplyFinishFB();
        } else {
            socialApplyLoginFB();
        }
    });
}

function socialApplyFinishFB() {
    sabApplyHandler({
        socialSrc : 'fb'
    });
}

function socialApplyLoginFB() {
    FB.login(function(response) {
        if (response.status === 'connected') {
            socialApplyFinishFB();
        }
    }, {
        scope: 'email'
    });
}

// *********************************************************************
// ** LinkedIn-specific Functions
// *********************************************************************

function sabLinkedInOnLoad() {
    // On a LinkedIn auth event, which happens when a user logs in (but also when an already logged-in user visits the page), check to see if the auth came as a result of a user apply action or not.
    IN.Event.on(IN,'auth',function() {
        socialApplyLoginLI();
    });
}

function socialApplyCheckLI() {
    // If the user is correctly logged in, finish the apply. Otherwise, the user needs to authenticate first.
    if (typeof IN === 'object' && typeof IN.User === 'object' && IN.User.isAuthorized()) {
        socialApplyFinishLI();
    } else {
        IN.User.authorize();
    }
}

function socialApplyFinishLI() {
    sabApplyHandler({
        socialSrc : 'li'
    });
}

function socialApplyLoginLI() {
    // If this auth event was fired because a user just logged in, then check to see if it is OK to finish the apply.
    if (j2w.SAB.li.applyStarted) {
        socialApplyCheckLI();
    }
}